#include "StdAfx.h"
#include "buffer.h"
#include "encoding.h"
#include <sstream>

namespace gfutilities{ namespace memory
{
	using namespace text;

//-------------------------------------------------------------------------------
buffer::buffer( size_t size, const byte* data /*= NULL*/ )
{
	// if there's data, copy the data with given size
	if(data)
	{
		_data.assign((const char*)data, size);
	}
	else if(size > 0)
	{
		_data.resize(size);
	}
}
//-------------------------------------------------------------------------------
buffer::buffer( const buffer& other )
{
	*this = other;
}
//-------------------------------------------------------------------------------
size_t buffer::get_size( void ) const
{
	return _data.size();
}
//-------------------------------------------------------------------------------
byte* buffer::get_data( void )
{
	return (byte*)const_cast<char*>(_data.c_str());
}
//-------------------------------------------------------------------------------
bool buffer::get_empty( void ) const
{
	return _data.empty();
}
//-------------------------------------------------------------------------------
void buffer::append( const byte* pdata, size_t dsize )
{
	if(dsize == 0){
		return;
	}

	std::string other;
	other.assign((const char*)pdata, dsize);
	_data += other;
}
//-------------------------------------------------------------------------------
void buffer::assign( const byte* pdata, size_t dsize )
{
	_data.assign((const char*)pdata, dsize);
}
//-------------------------------------------------------------------------------
size_t buffer::resize( size_t new_size )
{
	size_t old_size = _data.size();
	_data.resize(new_size);

	return old_size;
}
//-------------------------------------------------------------------------------
void buffer::clear()
{
	_data.clear();
}
//-------------------------------------------------------------------------------
buffer& buffer::operator=( const buffer& other )
{
	_data = other._data;
	return *this;
}
//-------------------------------------------------------------------------------
bool buffer::operator==( const buffer& other ) const
{
	return _data == other._data;
}
//-------------------------------------------------------------------------------
bool buffer::operator!=( const buffer& other ) const
{
	return _data != other._data;
}
//-------------------------------------------------------------------------------
buffer& buffer::operator+=( const buffer& other )
{
	_data += other._data;
	return *this;
}
//-------------------------------------------------------------------------------
buffer::operator byte*() const
{
	return (byte*)_data.c_str();
}
//-------------------------------------------------------------------------------
buffer::operator const byte*() const
{
	return (byte*)_data.c_str();
}
//-------------------------------------------------------------------------------
std::string buffer::get_as_string() const
{
	std::ostringstream converter;
	size_t len = _data.size();
	for(size_t i=0 ; i < len ; i++)
	{
		byte d = _data[i];
		if(d < 0x10){
			converter << 0;
		}

		converter << std::hex << static_cast<int>(d);
	}

	return converter.str();
}
//-------------------------------------------------------------------------------
std::wstring buffer::get_as_wstring() const
{
	return encoding::utf8_to_unicode(as_string);
}
//-------------------------------------------------------------------------------
}}